/*
 * Copyright 2010 Okan Kahraman
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package tr.org.tx.vmot.ui;

import org.springframework.security.core.context.SecurityContextHolder;

import tr.org.tx.vmot.ui.login.LoginView;

import com.vaadin.Application;
import com.vaadin.ui.Component.Event;
import com.vaadin.ui.Window;

/**
 * 
 * @author Okan Kahraman
 *
 */
@SuppressWarnings("serial")
public class VmotApplication extends Application
{
    private Window mainWindow;
    
    private Window loginWindow;
    
    private LoginView loginView;
    
    private MainView mainView;
      
	@Override
    public void init() {
		
		
		//mainWindow = new Window("Vaadin Motivation Calendar");

		//mainWindow.getContent().setSizeFull();
        //mainWindow.addComponent(new MainView());
        //setMainWindow(mainWindow);
        //setTheme("vmot");
        
		loginWindow = new Window("VMOT");
		
		loginView = new LoginView();
		loginView.setSizeFull();
		
		loginWindow.getContent().setSizeFull();
		loginWindow.addComponent(loginView);
        setMainWindow(loginWindow);
        setTheme("vmot");
        
 		loginView.addListener(new com.vaadin.ui.Component.Listener() {
			@Override
			public void componentEvent(Event event) {
				if (event instanceof LoginView.LoginEvent) {
					
					 
					setUser(((LoginView.LoginEvent) event).getAuthentication());
					SecurityContextHolder.getContext().setAuthentication(
						((LoginView.LoginEvent) event).getAuthentication());
					removeWindow(loginWindow);
					loginView = null;
					mainView = new MainView();
					mainView.setSizeFull();
					setMainWindow(new Window("VMOT",mainView));
				}
			}
		});
	}
       
    
}
